package com.openingedu.mba.service;

import java.sql.SQLException;
import java.util.Date;

import com.openingedu.mba.common.CommConsts;
import com.openingedu.mba.persist.DataHelper;
import com.openingedu.mba.persist.DbMessage;

import android.content.Context;
import android.util.Log;

public class CommService {

	private static final String TAG = CommService.class.getSimpleName();

	/**
	 * 初始化本地消息
	 * @param mContext 
	 */
	public void initLocalMessage(Context context) {
		Date now = new Date();
		DbMessage dbMessage = new DbMessage();
		// 本地第一条消息(欢迎消息)
		dbMessage.setMessageID(-100);
		dbMessage.setType(CommConsts.Message.local);
		dbMessage.setRead(false);
		dbMessage.setCreateTime(now);
		dbMessage.setTimeOutTime(now);
		dbMessage.setContent("欢迎使用综合宝典");
		try {
			DataHelper dataHelper = new DataHelper(context);
			dataHelper.getDbMessageDao().create(dbMessage);
		} catch (SQLException e) {
			e.printStackTrace();
			Log.i(TAG, "init local message failure. message=" + dbMessage);
		}
	}
	
	public void createMessage(Context context, String msg) {
		Date now = new Date();
		DbMessage dbMessage = new DbMessage();
		// 本地第一条消息(欢迎消息)
		dbMessage.setMessageID(-101);
		dbMessage.setType(CommConsts.Message.local);
		dbMessage.setRead(false);
		dbMessage.setCreateTime(now);
		dbMessage.setTimeOutTime(now);
		dbMessage.setContent(msg);
		try {
			DataHelper dataHelper = new DataHelper(context);
			dataHelper.getDbMessageDao().create(dbMessage);
		} catch (SQLException e) {
			e.printStackTrace();
			Log.i(TAG, "create message failure. message=" + dbMessage);
		}
	}

}
